package com.lx.lib.activity;

import android.os.SystemClock;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.lx.commonlib.mvp.persenter.BasePresenter;
import com.lx.commonlib.mvp.view.activity.ToolbarActivity;
import com.lx.commonlib.utils.ThreadUtil;
import com.lx.lib.R;

import java.util.List;

public class ThreadUtilsActivity extends ToolbarActivity {
    private static final String TAG="xxxxxxx"+ThreadUtilsActivity.class.getSimpleName();

    Runnable mRunnable;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_thread_utils;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("ThreadUtils");

        findViewById(R.id.tv02).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRunnable=new Runnable() {
                    @Override
                    public void run() {
                        for(int i=0;i<50;i++){
                            SystemClock.sleep(200);
                            Log.d(TAG,"i="+i);
                        }
                    }
                };
                Log.d(TAG,"开始新任务");
                Log.d(TAG,"mRunnable.hashCode()="+mRunnable.hashCode());
                ThreadUtil.getInstance().execute(mRunnable);
            }
        });
        findViewById(R.id.tv03).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ThreadUtil.getInstance().shutdown();
            }
        });
        findViewById(R.id.tv04).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Runnable> runnables = ThreadUtil.getInstance().shutdownNow();
                Log.d(TAG,"未开始任务个数:"+runnables.size());

            }
        });
        findViewById(R.id.tv05).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean remove = ThreadUtil.getInstance().remove(mRunnable);
                Log.d(TAG,remove?"移除了任务 mRunnable.hashCode()="+mRunnable.hashCode():"移除任务失败");
            }
        });
    }


}
